package com.coin900.coin.service.sop;

import com.coin900.coin.persist.sop.SopMenuVO;
import com.coin900.coin.persist.sop.SopRoleButtonVO;
import com.coin900.coin.persist.sop.SopRoleMenuVO;
import com.coin900.coin.persist.sop.SopRoleVO;
import com.value.mybatis.IDaoRouter;
import com.value.mybatis.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shui
 * @create 2018-01-05
 **/
@Service
public class SopRoleService implements ISopRoleService {

    @Override
    public List<SopRoleVO> loadSopRole(Map<String, Object> params) {
        return this.daoRouter.query("SopRole.loadSopRole", params);
    }

    @Override
    public List<SopRoleVO> loadSopRoleForPage(Pagination pagination) {
        return this.daoRouter.query("SopRole.loadSopRoleForPage", pagination);
    }

    @Override
    public void insertSopRole(SopRoleVO sopRoleVO) {
        this.daoRouter.insert("SopRole.insertSopRole", sopRoleVO);
    }

    @Override
    public void updateSopRole(SopRoleVO sopRoleVO) {
        this.daoRouter.update("SopRole.updateSopRole", sopRoleVO);
    }

    @Override
    public void updateSopRoleMenuAndButton(SopRoleVO sopRoleVO) {
        Long roleId = sopRoleVO.getId();
        // 删除角色所有的菜单
        SopRoleMenuVO sopRoleMenuVO = new SopRoleMenuVO();
        sopRoleMenuVO.setRoleId(roleId);
        this.roleMenuService.deleteSopRoleMenu(sopRoleMenuVO);
        // 删除角色所有的按钮
        SopRoleButtonVO sopRoleButtonVO = new SopRoleButtonVO();
        sopRoleButtonVO.setRoleId(roleId);
        this.roleButtonService.deleteSopRoleButton(sopRoleButtonVO);
        // 添加新的菜单
        this.roleMenuService.insertSopRoleMenuBatch(roleId, sopRoleVO.getSopMenuList());
        // 添加新的按钮
        this.roleButtonService.insertSopRoleButtonBatch(roleId, sopRoleVO.getSopButtonList());
    }

    @Override
    public void deleteSopRole(SopRoleVO sopRoleVO) {
        this.daoRouter.update("SopRole.deleteSopRole", sopRoleVO);
        Long roleId = sopRoleVO.getId();
        // 删除角色的按钮和菜单信息
        SopRoleMenuVO sopRoleMenuVO = new SopRoleMenuVO();
        sopRoleMenuVO.setRoleId(roleId);
        this.roleMenuService.deleteSopRoleMenu(sopRoleMenuVO);
        SopRoleButtonVO sopRoleButtonVO = new SopRoleButtonVO();
        sopRoleButtonVO.setRoleId(roleId);
        this.roleButtonService.deleteSopRoleButton(sopRoleButtonVO);
    }

    @Autowired
    private ISopRoleMenuService roleMenuService;
    @Autowired
    private ISopRoleButtonService roleButtonService;
    @Autowired
    private IDaoRouter daoRouter;

}